package com.chaoyong.higo.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chaoyong.higo.R;
import com.chaoyong.higo.activity.SharDetailActivity;
import com.chaoyong.higo.bean.ShowOrderEntity;
import com.chaoyong.higo.utils.DataTimeTools;
import com.chaoyong.higo.utils.EmptyUtil;
import com.chaoyong.higo.utils.Values;
import com.chaoyong.higo.view.RoundImageView;
import com.nostra13.universalimageloader.core.ImageLoader;

public class SharAdapter extends RecyclerView.Adapter<SharAdapter.MyViewHolder> {
	private Context context;
	private LayoutInflater inflater;
	private List<ShowOrderEntity> list;
	private SetOnClickListeners clickListeners;

	public SharAdapter(Context context, List<ShowOrderEntity> list) {
		super();
		this.list = list;
		this.context = context;
		inflater = inflater.from(context);
	}

	class MyViewHolder extends ViewHolder {
		LinearLayout item_shar_lay;
		RoundImageView imageView;
		TextView user_tx, name_tx, time_tx;
		ImageView shar_img1, shar_img2, shar_img3;

		public MyViewHolder(View arg0) {
			super(arg0);
			item_shar_lay = (LinearLayout) arg0.findViewById(R.id.item_shar_lay);
			imageView = (RoundImageView) arg0.findViewById(R.id.itemshar_user_lay);
			user_tx = (TextView) arg0.findViewById(R.id.shar_user_tx);
			name_tx = (TextView) arg0.findViewById(R.id.shar_name_tx);
			time_tx = (TextView) arg0.findViewById(R.id.shar_time_tx);
			shar_img1 = (ImageView) arg0.findViewById(R.id.shar_img1);
			shar_img2 = (ImageView) arg0.findViewById(R.id.shar_img2);
			shar_img3 = (ImageView) arg0.findViewById(R.id.shar_img3);
		}
	}

	@Override
	public int getItemCount() {
		return list.size();
	}

	@Override
	public void onBindViewHolder(MyViewHolder holder, final int arg1) {
		
		holder.name_tx.setText(list.get(arg1).getShare_title());
		holder.user_tx.setText(list.get(arg1).getUser_name());
		holder.time_tx.setText(new DataTimeTools().time2(list.get(arg1).getShare_time()));
		String uid_portrait = list.get(arg1).getUid_portrait();
		ImageLoader.getInstance().displayImage(Values.BASE_IMAGE_URL + uid_portrait, holder.imageView);
		List<String> uri = list.get(arg1).getFace_img();
		if (!EmptyUtil.isEmpty(uri)) {
			if (uri.size() == 1) {
				// ImageLoader.getInstance().displayImage(Values.BASE_IMAGE_URL
				// + uri.get(0), holder.shar_img1);
				ImageLoader.getInstance().displayImage(uri.get(0), holder.shar_img1);
				holder.shar_img2.setVisibility(View.INVISIBLE);
				holder.shar_img3.setVisibility(View.INVISIBLE);
			} else if (uri.size() == 2) {
				// ImageLoader.getInstance().displayImage(Values.BASE_IMAGE_URL
				// + uri.get(0), holder.shar_img1);
				// ImageLoader.getInstance().displayImage(Values.BASE_IMAGE_URL
				// + uri.get(1), holder.shar_img2);
				ImageLoader.getInstance().displayImage(uri.get(0), holder.shar_img1);
				ImageLoader.getInstance().displayImage(uri.get(1), holder.shar_img2);
				holder.shar_img3.setVisibility(View.INVISIBLE);
			} else if (uri.size() == 3) {
				// ImageLoader.getInstance().displayImage(Values.BASE_IMAGE_URL
				// + uri.get(0), holder.shar_img1);
				// ImageLoader.getInstance().displayImage(Values.BASE_IMAGE_URL
				// + uri.get(1), holder.shar_img2);
				// ImageLoader.getInstance().displayImage(Values.BASE_IMAGE_URL
				// + uri.get(2), holder.shar_img3);
				ImageLoader.getInstance().displayImage(uri.get(0), holder.shar_img1);
				ImageLoader.getInstance().displayImage(uri.get(1), holder.shar_img2);
				ImageLoader.getInstance().displayImage(uri.get(2), holder.shar_img3);
			}
		}
		
		holder.item_shar_lay.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i1 = new Intent(context, SharDetailActivity.class);
				ShowOrderEntity soe = list.get(arg1);
				i1.putExtra("sharEntity", soe);
				context.startActivity(i1);
//				if (clickListeners != null) {
//					clickListeners.OnClick();
//				}
			}
		});

	}

	@Override
	public MyViewHolder onCreateViewHolder(ViewGroup arg0, int arg1) {
		MyViewHolder holder = new MyViewHolder(inflater.inflate(R.layout.item_shar, arg0, false));
		return holder;
	}

	public void HolderClickListener(SetOnClickListeners clickListeners) {

		this.clickListeners = clickListeners;
	}

	public interface SetOnClickListeners {
		public void OnClick();
	}

}
